package cn.edu.ctbu.web01.api;

import cn.edu.ctbu.web01.entity.Teacher;
import cn.edu.ctbu.web01.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/teacher")
public class TeacherApiController {

    @Autowired
    private TeacherService teacherService;

    /**
     * 读取所有的教师信息
     *
     * @return
     */
    @GetMapping("/list")
    public List<Teacher> findAll() {
        List<Teacher> teachers = teacherService.getAll();
        return teachers;
    }

    /**
     * 按照id查询
     * @param id
     * @return
     */
    @GetMapping("{id}")
    public Teacher findById(@PathVariable int id) {
        Teacher teacher = teacherService.findById(id);
        return teacher;
    }

    /**
     * 添加教师
     * @param teacher
     * @return
     */
    @PostMapping("/add")
    public Teacher add(Teacher teacher) {
        return teacherService.add(teacher);
    }

    /**
     * 更新教师
     * @param teacher
     * @return
     */
    @PutMapping("/update")
    public Teacher update(Teacher teacher) {
        return teacherService.update(teacher);
    }

    /**
     * 按id删除教师信息
     * @param id
     */
    @DeleteMapping("/delete/{id}")
    public void delete(@PathVariable Integer id) {
        teacherService.delete(id);
    }

}
